package com.bishe.bishe.Service.ServiceImpl;

import com.bishe.bishe.Service.OrderService;
import com.bishe.bishe.dao.mapper.OrderMapper;
import com.bishe.bishe.dao.mapper.OrderdetailMapper;
import com.bishe.bishe.dao.mapper.StateMapper;
import com.bishe.bishe.pojo.Advertisements;
import com.bishe.bishe.pojo.Customer;
import com.bishe.bishe.pojo.Orders;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderdetailMapper orderdetailMapper;
    @Autowired
    private StateMapper stateMapper;

    /*查询所有订单，并且分页*/
    @Override
    public  PageInfo<Orders> findall(int page, int size) {
        PageHelper.startPage(page,size);
        List<Orders> list=orderMapper.findall();

        // 封装list到PageInfo对象中自动分页
        PageInfo<Orders> poPageInfo = new PageInfo<>(list);
        return poPageInfo;
    }
    /*查询time时间/名字/订单号的所有订单，并且分页*/
    @Override
    public PageInfo <Orders> findPage(Map<String, Object> searchMap, int page, int size,String stateName) {
        if (searchMap!=null){
            if (searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                return  new PageInfo<>(orderMapper.finddetailByRandom(searchMap.get("name"),stateName));
            }else {
                return null;
            }
        }else {
            return null;
        }
    }

    /*更新，主要用于修改状态*/
    @Override
    public int update(Orders orders) {
        if (orders.getState().getStateName().equals("已完成")){
            java.util.Date date=new java.util.Date();
            long l = date.getTime();
            java.sql.Date sqlDate=new java.sql.Date(l);
            orders.setOrderDate(sqlDate);
            return orderMapper.updateStateDate(orders);
        }else if (orders.getState().getStateName()=="申请取消"){
            return orderMapper.updateReason(orders);
        }else {
            return orderMapper.updateState(orders);
        }
}

    /*更新，主要用于修改状态*/
    @Override
    public int updateAdvertisement(String advertisement) {
        return orderMapper.updateAdvertisement(advertisement);
    }

    @Override
    public  List<Advertisements> selectAdvert() {
        return orderMapper.selectAdvert();
    }

    @Override
    public List <Orders> findByWx(String customerNumber) {
        return orderMapper.findByWx(customerNumber);
    }

    @Override
    public int insert(Orders orders) {
        return orderMapper.insert(orders);
    }

    @Override
    public int indexinsert(Orders orders) {
        return orderMapper.indexinsert(orders);
    }

    @Override
    public int findexpress() {
        List findexpress = orderMapper.findexpress();
        return findexpress.size();
    }

    @Override
    public Orders findByIdcheckId(Long orderId, int checkId) {
        return orderMapper.findByIdcheckId(orderId, checkId);
    }

    @Override
    public List<Orders> count() {
        return orderMapper.count();
    }

    @Override
    public PageInfo <Orders> findstate(String stateName, int page, int size) {
        PageHelper.startPage(page, size);
        List<Orders> list=orderMapper.findorderByName(stateName);
        PageInfo<Orders> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public List<Orders> findstate(String stateName) {
        List list = orderMapper.findorderByName(stateName);
        return list;
    }

    @Override
    public Orders findById(Orders orders) {
        return (Orders) orderMapper.selectByPrimaryKey(orders);
    }

    @Override
    public List <Orders> findByCheck(String customerNumber, int orderIdentify) {
        Customer customer=new Customer();
        customer.setCustomerNumber(customerNumber);
        Orders orders=new Orders();
        orders.setCustomer(customer);
        List list = orderMapper.findByNumber(customerNumber,orderIdentify);
        return list;
    }


    @Override
    public int delete(Orders orders) {
        return orderMapper.deleteByPrimaryKey(orders);
    }




}
